
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>组件交互 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="style.html" />
    
    
    <link rel="prev" href="cycle.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../Angular-cli/">
            
                <a href="../Angular-cli/">
            
                    
                    Angular-cli
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../Angular-cli/Angular-cli.html">
            
                <a href="../Angular-cli/Angular-cli.html">
            
                    
                    安装
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../Angular-cli/restMenu.html">
            
                <a href="../Angular-cli/restMenu.html">
            
                    
                    初始结构
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../architecture/">
            
                <a href="../architecture/">
            
                    
                    架构
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../architecture/mo-kuai.html">
            
                <a href="../architecture/mo-kuai.html">
            
                    
                    模块简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../architecture/component.html">
            
                <a href="../architecture/component.html">
            
                    
                    组件简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../architecture/Service.html">
            
                <a href="../architecture/Service.html">
            
                    
                    服务与依赖注入
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="./">
            
                <a href="./">
            
                    
                    组件与模板
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="zu-jian-yu-mo-ban.html">
            
                <a href="zu-jian-yu-mo-ban.html">
            
                    
                    数据显示与模板语法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="cycle.html">
            
                <a href="cycle.html">
            
                    
                    生命周期钩子
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.4.3" data-path="component.html">
            
                <a href="component.html">
            
                    
                    组件交互
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="style.html">
            
                <a href="style.html">
            
                    
                    组件样式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="dynamic.html">
            
                <a href="dynamic.html">
            
                    
                    动态组件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="directive.html">
            
                <a href="directive.html">
            
                    
                    属性指令与结构指令
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7" data-path="pipeAndAnimit.html">
            
                <a href="pipeAndAnimit.html">
            
                    
                    管道与动画
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../router/">
            
                <a href="../router/">
            
                    
                    路由与导航
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../form/">
            
                <a href="../form/">
            
                    
                    表单
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../http/">
            
                <a href="../http/">
            
                    
                    请求http
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../project/">
            
                <a href="../project/">
            
                    
                    项目解析:结构目录
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >组件交互</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h3 id="&#x7EC4;&#x4EF6;&#x4E4B;&#x95F4;&#x7684;&#x4EA4;&#x4E92;">&#x7EC4;&#x4EF6;&#x4E4B;&#x95F4;&#x7684;&#x4EA4;&#x4E92;</h3>
<ul>
<li>&#x901A;&#x8FC7;&#x8F93;&#x5165;&#x578B;&#x7ED1;&#x5B9A;&#x628A;&#x6570;&#x636E;&#x4ECE;&#x7236;&#x7EC4;&#x4EF6;&#x4F20;&#x5230;&#x5B50;&#x7EC4;&#x4EF6;.</li>
</ul>
<p>&#x5B50;&#x7EC4;&#x4EF6;&#x7528; <code>@Input</code> &#x88C5;&#x9970;&#x5668;&#x3002;</p>
<p>&#x5B50;&#x7EC4;&#x4EF6;</p>
<pre><code>    import { Component, Input } from &apos;@angular/core&apos;;

    import { Hero } from &apos;./hero&apos;;

    @Component({
      selector: &apos;app-hero-child&apos;,
      template: `
        &lt;h3&gt; says:&lt;/h3&gt;
        &lt;p&gt;I, , am at your service, .&lt;/p&gt;
      `
    })
    export class HeroChildComponent {
      @Input() hero: Hero;
      @Input(&apos;master&apos;) masterName: string;
}
</code></pre><p> &#x7236;&#x7EC4;&#x4EF6;&#x628A;&#x81EA;&#x5DF1;&#x7684; hero &#x5B9E;&#x4F8B;&#x7ED1;&#x5B9A;&#x5230;&#x5B50;&#x7EC4;&#x4EF6;&#x7684; hero &#x5C5E;&#x6027;&#x3002; <code>[hero]=&quot;hero&quot;</code></p>
<p> &#x7236;&#x7EC4;&#x4EF6;</p>
<pre><code>     import { Component } from &apos;@angular/core&apos;;

     import { HEROES } from &apos;./hero&apos;;

     @Component({
       selector: &apos;app-hero-parent&apos;,
       template: `
         &lt;h2&gt; controls  heroes&lt;/h2&gt;
         &lt;app-hero-child *ngFor=&quot;let hero of heroes&quot;
           [hero]=&quot;hero&quot;
           [master]=&quot;master&quot;&gt;
         &lt;/app-hero-child&gt;
       `
     })
     export class HeroParentComponent {
       heroes = HEROES;
       master = &apos;Master&apos;;
     }
</code></pre><ul>
<li>&#x901A;&#x8FC7; <code>setter</code> &#x622A;&#x542C;&#x8F93;&#x5165;&#x5C5E;&#x6027;&#x503C;&#x7684;&#x53D8;&#x5316;</li>
</ul>
<p>&#x4F7F;&#x7528;&#x4E00;&#x4E2A;&#x8F93;&#x5165;&#x5C5E;&#x6027;&#x7684; <code>setter</code>&#xFF0C;&#x4EE5;&#x62E6;&#x622A;&#x7236;&#x7EC4;&#x4EF6;&#x4E2D;&#x503C;&#x7684;&#x53D8;&#x5316;&#xFF0C;&#x5E76;&#x91C7;&#x53D6;&#x884C;&#x52A8;&#x3002;</p>
<p>&#x5B50;&#x7EC4;&#x4EF6;&#x7684;&#x8F93;&#x5165;&#x5C5E;&#x6027; name &#x4E0A;&#x7684;&#x8FD9;&#x4E2A; <code>setter</code>&#xFF0C;&#x4F1A; <code>trim</code> &#x6389;&#x540D;&#x5B57;&#x91CC;&#x7684;&#x7A7A;&#x683C;&#xFF0C;&#x5E76;&#x628A;&#x7A7A;&#x503C;&#x66FF;&#x6362;&#x6210;&#x9ED8;&#x8BA4;&#x5B57;&#x7B26;&#x4E32;&#x3002;</p>
<pre><code>    import { Component, Input } from &apos;@angular/core&apos;;

    @Component({
      selector: &apos;app-name-child&apos;,
      template: &apos;&lt;h3&gt;&quot;&quot;&lt;/h3&gt;&apos;
    })
    export class NameChildComponent {
      private _name = &apos;&apos;;

      @Input()
      set name(name: string) {
        this._name = (name &amp;&amp; name.trim()) || &apos;&lt;no name set&gt;&apos;;
      }

      get name(): string { return this._name; }
    }
</code></pre><ul>
<li><p>&#x901A;&#x8FC7;<code>ngOnChanges()</code>&#x6765;&#x622A;&#x542C;&#x8F93;&#x5165;&#x5C5E;&#x6027;&#x503C;&#x7684;&#x53D8;&#x5316;</p>
<p>&#x4F7F;&#x7528; <code>OnChanges</code> &#x751F;&#x547D;&#x5468;&#x671F;&#x94A9;&#x5B50;&#x63A5;&#x53E3;&#x7684; <code>ngOnChanges()</code> &#x65B9;&#x6CD5;&#x6765;&#x76D1;&#x6D4B;&#x8F93;&#x5165;&#x5C5E;&#x6027;&#x503C;&#x7684;&#x53D8;&#x5316;&#x5E76;&#x505A;&#x51FA;&#x56DE;&#x5E94;&#x3002;</p>
</li>
</ul>
<pre><code>    import { Component, Input, OnChanges, SimpleChange } from &apos;@angular/core&apos;;

    @Component({
      selector: &apos;app-version-child&apos;,
      template: `
        &lt;h3&gt;Version .&lt;/h3&gt;
        &lt;h4&gt;Change log:&lt;/h4&gt;
        &lt;ul&gt;
          &lt;li *ngFor=&quot;let change of changeLog&quot;&gt;&lt;/li&gt;
        &lt;/ul&gt;
      `
    })
    export class VersionChildComponent implements OnChanges {
      @Input() major: number;
      @Input() minor: number;
      changeLog: string[] = [];

      ngOnChanges(changes: {[propKey: string]: SimpleChange}) {
        let log: string[] = [];
        for (let propName in changes) {
          let changedProp = changes[propName];
          let to = JSON.stringify(changedProp.currentValue);
          if (changedProp.isFirstChange()) {
            log.push(`Initial value of ${propName} set to ${to}`);
          } else {
            let from = JSON.stringify(changedProp.previousValue);
            log.push(`${propName} changed from ${from} to ${to}`);
          }
        }
        this.changeLog.push(log.join(&apos;, &apos;));
      }
    }
</code></pre><ul>
<li>&#x7236;&#x7EC4;&#x4EF6;&#x76D1;&#x542C;&#x5B50;&#x7EC4;&#x4EF6;&#x7684;&#x4E8B;&#x4EF6;</li>
</ul>
<p>&#x5B50;&#x7EC4;&#x4EF6;&#x66B4;&#x9732;&#x4E00;&#x4E2A; <code>EventEmitter</code> &#x5C5E;&#x6027;&#xFF0C;&#x5F53;&#x4E8B;&#x4EF6;&#x53D1;&#x751F;&#x65F6;&#xFF0C;&#x5B50;&#x7EC4;&#x4EF6;&#x5229;&#x7528;&#x8BE5;&#x5C5E;&#x6027; <code>emits</code>(&#x5411;&#x4E0A;&#x5F39;&#x5C04;)&#x4E8B;&#x4EF6;&#x3002;&#x7236;&#x7EC4;&#x4EF6;&#x7ED1;&#x5B9A;&#x5230;&#x8FD9;&#x4E2A;&#x4E8B;&#x4EF6;&#x5C5E;&#x6027;&#xFF0C;&#x5E76;&#x5728;&#x4E8B;&#x4EF6;&#x53D1;&#x751F;&#x65F6;&#x4F5C;&#x51FA;&#x56DE;&#x5E94;&#x3002;</p>
<p>&#x5B50;&#x7EC4;&#x4EF6;&#x7684; <code>EventEmitter</code> &#x5C5E;&#x6027;&#x662F;&#x4E00;&#x4E2A;&#x8F93;&#x51FA;&#x5C5E;&#x6027;&#xFF0C;&#x901A;&#x5E38;&#x5E26;&#x6709;<code>@Output</code> &#x88C5;&#x9970;&#x5668;&#x3002;</p>
<pre><code>    import { Component, EventEmitter, Input, Output } from &apos;@angular/core&apos;;

    @Component({
      selector: &apos;app-voter&apos;,
      template: `
        &lt;h4&gt;&lt;/h4&gt;
        &lt;button (click)=&quot;vote(true)&quot;  [disabled]=&quot;voted&quot;&gt;Agree&lt;/button&gt;
        &lt;button (click)=&quot;vote(false)&quot; [disabled]=&quot;voted&quot;&gt;Disagree&lt;/button&gt;
      `
    })
    export class VoterComponent {
      @Input()  name: string;
      @Output() onVoted = new EventEmitter&lt;boolean&gt;();
      voted = false;

      vote(agreed: boolean) {
        this.onVoted.emit(agreed);
        this.voted = true;
      }
    }
</code></pre><p>&#x7236;&#x7EC4;&#x4EF6; &#x7ED1;&#x5B9A;&#x4E86;&#x4E00;&#x4E2A;&#x4E8B;&#x4EF6;&#x5904;&#x7406;&#x5668;<code>(onVoted())</code>&#xFF0C;&#x7528;&#x6765;&#x54CD;&#x5E94;&#x5B50;&#x7EC4;&#x4EF6;&#x7684;&#x4E8B;&#x4EF6;<code>($event)</code>&#x5E76;&#x66F4;&#x65B0;&#x4E00;&#x4E2A;&#x8BA1;&#x6570;&#x5668;&#x3002;</p>
<pre><code>    import { Component }      from &apos;@angular/core&apos;;

    @Component({
      selector: &apos;app-vote-taker&apos;,
      template: `
        &lt;h2&gt;Should mankind colonize the Universe?&lt;/h2&gt;
        &lt;h3&gt;Agree: , Disagree: &lt;/h3&gt;
        &lt;app-voter *ngFor=&quot;let voter of voters&quot;
          [name]=&quot;voter&quot;
          (onVoted)=&quot;onVoted($event)&quot;&gt;
        &lt;/app-voter&gt;
      `
    })
    export class VoteTakerComponent {
      agreed = 0;
      disagreed = 0;
      voters = [&apos;Mr. IQ&apos;, &apos;Ms. Universe&apos;, &apos;Bombasto&apos;];

      onVoted(agreed: boolean) {
        agreed ? this.agreed++ : this.disagreed++;
      }
    }
</code></pre><ul>
<li>&#x7236;&#x7EC4;&#x4EF6;&#x4E0E;&#x5B50;&#x7EC4;&#x4EF6;&#x901A;&#x8FC7;&#x672C;&#x5730;&#x53D8;&#x91CF;&#x4E92;&#x52A8;</li>
</ul>
<p>&#x7236;&#x7EC4;&#x4EF6;&#x4E0D;&#x80FD;&#x4F7F;&#x7528;&#x6570;&#x636E;&#x7ED1;&#x5B9A;&#x6765;&#x8BFB;&#x53D6;&#x5B50;&#x7EC4;&#x4EF6;&#x7684;&#x5C5E;&#x6027;&#x6216;&#x8C03;&#x7528;&#x5B50;&#x7EC4;&#x4EF6;&#x7684;&#x65B9;&#x6CD5;&#x3002;&#x4F46;&#x53EF;&#x4EE5;&#x5728;&#x7236;&#x7EC4;&#x4EF6;&#x6A21;&#x677F;&#x91CC;&#xFF0C;&#x65B0;&#x5EFA;&#x4E00;&#x4E2A;&#x672C;&#x5730;&#x53D8;&#x91CF;&#x6765;&#x4EE3;&#x8868;&#x5B50;&#x7EC4;&#x4EF6;&#xFF0C;&#x7136;&#x540E;&#x5229;&#x7528;&#x8FD9;&#x4E2A;&#x53D8;&#x91CF;&#x6765;&#x8BFB;&#x53D6;&#x5B50;&#x7EC4;&#x4EF6;&#x7684;&#x5C5E;&#x6027;&#x548C;&#x8C03;&#x7528;&#x5B50;&#x7EC4;&#x4EF6;&#x7684;&#x65B9;&#x6CD5;&#xFF0C;&#x5982;&#x4E0B;&#x4F8B;&#x6240;&#x793A;&#x3002;</p>
<pre><code>    import { Component, OnDestroy, OnInit } from &apos;@angular/core&apos;;

    @Component({
      selector: &apos;app-countdown-timer&apos;,
      template: &apos;&lt;p&gt;&lt;/p&gt;&apos;
    })
    export class CountdownTimerComponent implements OnInit, OnDestroy {

      intervalId = 0;
      message = &apos;&apos;;
      seconds = 11;

      clearTimer() { clearInterval(this.intervalId); }

      ngOnInit()    { this.start(); }
      ngOnDestroy() { this.clearTimer(); }

      start() { this.countDown(); }
      stop()  {
        this.clearTimer();
        this.message = `Holding at T-${this.seconds} seconds`;
      }

      private countDown() {
        this.clearTimer();
        this.intervalId = window.setInterval(() =&gt; {
          this.seconds -= 1;
          if (this.seconds === 0) {
            this.message = &apos;Blast off!&apos;;
          } else {
            if (this.seconds &lt; 0) { this.seconds = 10; } // reset
            this.message = `T-${this.seconds} seconds and counting`;
          }
        }, 1000);
      }
    }
</code></pre><p>&#x8BA1;&#x65F6;&#x5668;&#x7EC4;&#x4EF6;&#x7684;&#x5BBF;&#x4E3B;&#x7EC4;&#x4EF6;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code>    import { Component }                from &apos;@angular/core&apos;;
    import { CountdownTimerComponent }  from &apos;./countdown-timer.component&apos;;

    @Component({
      selector: &apos;app-countdown-parent-lv&apos;,
      template: `
      &lt;h3&gt;Countdown to Liftoff (via local variable)&lt;/h3&gt;
      &lt;button (click)=&quot;timer.start()&quot;&gt;Start&lt;/button&gt;
      &lt;button (click)=&quot;timer.stop()&quot;&gt;Stop&lt;/button&gt;
      &lt;div class=&quot;seconds&quot;&gt;&lt;/div&gt;
      &lt;app-countdown-timer #timer&gt;&lt;/app-countdown-timer&gt;
      `,
      styleUrls: [&apos;../assets/demo.css&apos;]
    })
    export class CountdownLocalVarParentComponent { }
</code></pre><ul>
<li>&#x7236;&#x7EC4;&#x4EF6;&#x8C03;&#x7528;<code>@ViewChild()</code></li>
</ul>
<p>&#x8FD9;&#x4E2A;&#x672C;&#x5730;&#x53D8;&#x91CF;&#x65B9;&#x6CD5;&#x662F;&#x4E2A;&#x7B80;&#x5355;&#x4FBF;&#x5229;&#x7684;&#x65B9;&#x6CD5;&#x3002;&#x4F46;&#x662F;&#x5B83;&#x4E5F;&#x6709;&#x5C40;&#x9650;&#x6027;&#xFF0C;&#x56E0;&#x4E3A;&#x7236;&#x7EC4;&#x4EF6;-&#x5B50;&#x7EC4;&#x4EF6;&#x7684;&#x8FDE;&#x63A5;&#x5FC5;&#x987B;&#x5168;&#x90E8;&#x5728;&#x7236;&#x7EC4;&#x4EF6;&#x7684;&#x6A21;&#x677F;&#x4E2D;&#x8FDB;&#x884C;&#x3002;&#x7236;&#x7EC4;&#x4EF6;&#x672C;&#x8EAB;&#x7684;&#x4EE3;&#x7801;&#x5BF9;&#x5B50;&#x7EC4;&#x4EF6;&#x6CA1;&#x6709;&#x8BBF;&#x95EE;&#x6743;&#x3002;</p>
<p>&#x5982;&#x679C;&#x7236;&#x7EC4;&#x4EF6;&#x7684;&#x7C7B;&#x9700;&#x8981;&#x8BFB;&#x53D6;&#x5B50;&#x7EC4;&#x4EF6;&#x7684;&#x5C5E;&#x6027;&#x503C;&#x6216;&#x8C03;&#x7528;&#x5B50;&#x7EC4;&#x4EF6;&#x7684;&#x65B9;&#x6CD5;&#xFF0C;&#x5C31;&#x4E0D;&#x80FD;&#x4F7F;&#x7528;&#x672C;&#x5730;&#x53D8;&#x91CF;&#x65B9;&#x6CD5;&#x3002;</p>
<p>&#x5F53;&#x7236;&#x7EC4;&#x4EF6;&#x7C7B;&#x9700;&#x8981;&#x8FD9;&#x79CD;&#x8BBF;&#x95EE;&#x65F6;&#xFF0C;&#x53EF;&#x4EE5;&#x628A;&#x5B50;&#x7EC4;&#x4EF6;&#x4F5C;&#x4E3A; <code>ViewChild</code>&#xFF0C;&#x6CE8;&#x5165;&#x5230;&#x7236;&#x7EC4;&#x4EF6;&#x91CC;&#x9762;&#x3002;</p>
<p>&#x4E0A;&#x4F8B;&#x5B50;&#x7EC4;&#x4EF6;&#x4E0D;&#x53D8;&#xFF0C;&#x66F4;&#x6539;&#x7236;&#x7EC4;&#x4EF6;</p>
<pre><code>    import { AfterViewInit, ViewChild } from &apos;@angular/core&apos;;
    import { Component }                from &apos;@angular/core&apos;;
    import { CountdownTimerComponent }  from &apos;./countdown-timer.component&apos;;

    @Component({
      selector: &apos;app-countdown-parent-vc&apos;,
      template: `
      &lt;h3&gt;Countdown to Liftoff (via ViewChild)&lt;/h3&gt;
      &lt;button (click)=&quot;start()&quot;&gt;Start&lt;/button&gt;
      &lt;button (click)=&quot;stop()&quot;&gt;Stop&lt;/button&gt;
      &lt;div class=&quot;seconds&quot;&gt;&lt;/div&gt;
      &lt;app-countdown-timer&gt;&lt;/app-countdown-timer&gt;
      `,
      styleUrls: [&apos;../assets/demo.css&apos;]
    })
    export class CountdownViewChildParentComponent implements AfterViewInit {

      @ViewChild(CountdownTimerComponent)
      private timerComponent: CountdownTimerComponent;

      seconds() { return 0; }

      ngAfterViewInit() {
        // Redefine `seconds()` to get from the `CountdownTimerComponent.seconds` ...
        // but wait a tick first to avoid one-time devMode
        // unidirectional-data-flow-violation error
        setTimeout(() =&gt; this.seconds = () =&gt; this.timerComponent.seconds, 0);
      }

      start() { this.timerComponent.start(); }
      stop() { this.timerComponent.stop(); }
    }
</code></pre><p>   &#x628A;&#x5B50;&#x7EC4;&#x4EF6;&#x7684;&#x89C6;&#x56FE;&#x63D2;&#x5165;&#x5230;&#x7236;&#x7EC4;&#x4EF6;&#x7C7B;&#x9700;&#x8981;&#x505A;&#x4E00;&#x70B9;&#x989D;&#x5916;&#x7684;&#x5DE5;&#x4F5C;&#x3002;</p>
<p>   &#x9996;&#x5148;&#xFF0C;&#x4F60;&#x8981;&#x4F7F;&#x7528; <code>ViewChild</code> &#x88C5;&#x9970;&#x5668;&#x5BFC;&#x5165;&#x8FD9;&#x4E2A;&#x5F15;&#x7528;&#xFF0C;&#x5E76;&#x6302;&#x4E0A; AfterViewInit &#x751F;&#x547D;&#x5468;&#x671F;&#x94A9;&#x5B50;&#x3002;</p>
<p>   &#x63A5;&#x7740;&#xFF0C;&#x901A;&#x8FC7; <code>@ViewChild</code> &#x5C5E;&#x6027;&#x88C5;&#x9970;&#x5668;&#xFF0C;&#x5C06;&#x5B50;&#x7EC4;&#x4EF6; <code>CountdownTimerComponent</code> &#x6CE8;&#x5165;&#x5230;&#x79C1;&#x6709;&#x5C5E;&#x6027; <code>timerComponent</code> &#x91CC;&#x9762;&#x3002;</p>
<p>   &#x7EC4;&#x4EF6;&#x5143;&#x6570;&#x636E;&#x91CC;&#x5C31;&#x4E0D;&#x518D;&#x9700;&#x8981; <code>#timer</code> &#x672C;&#x5730;&#x53D8;&#x91CF;&#x4E86;&#x3002;&#x800C;&#x662F;&#x628A;&#x6309;&#x94AE;&#x7ED1;&#x5B9A;&#x5230;&#x7236;&#x7EC4;&#x4EF6;&#x81EA;&#x5DF1;&#x7684; <code>start</code> &#x548C; <code>stop</code> &#x65B9;&#x6CD5;&#xFF0C;&#x4F7F;&#x7528;&#x7236;&#x7EC4;&#x4EF6;&#x7684; <code>seconds</code> &#x65B9;&#x6CD5;&#x7684;&#x63D2;&#x503C;&#x8868;&#x8FBE;&#x5F0F;&#x6765;&#x5C55;&#x793A;&#x79D2;&#x6570;&#x53D8;&#x5316;&#x3002;</p>
<p>   &#x8FD9;&#x4E9B;&#x65B9;&#x6CD5;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x8BBF;&#x95EE;&#x88AB;&#x6CE8;&#x5165;&#x7684;&#x8BA1;&#x65F6;&#x5668;&#x7EC4;&#x4EF6;&#x3002;</p>
<p>   <code>ngAfterViewInit()</code> &#x751F;&#x547D;&#x5468;&#x671F;&#x94A9;&#x5B50;&#x662F;&#x975E;&#x5E38;&#x91CD;&#x8981;&#x7684;&#x4E00;&#x6B65;&#x3002;&#x88AB;&#x6CE8;&#x5165;&#x7684;&#x8BA1;&#x65F6;&#x5668;&#x7EC4;&#x4EF6;&#x53EA;&#x6709;&#x5728; Angular &#x663E;&#x793A;&#x4E86;&#x7236;&#x7EC4;&#x4EF6;&#x89C6;&#x56FE;&#x4E4B;&#x540E;&#x624D;&#x80FD;&#x8BBF;&#x95EE;&#xFF0C;&#x6240;&#x4EE5;&#x5B83;&#x5148;&#x628A;&#x79D2;&#x6570;&#x663E;&#x793A;&#x4E3A; 0.</p>
<p>   &#x7136;&#x540E; Angular &#x4F1A;&#x8C03;&#x7528; <code>ngAfterViewInit</code> &#x751F;&#x547D;&#x5468;&#x671F;&#x94A9;&#x5B50;&#xFF0C;&#x4F46;&#x8FD9;&#x65F6;&#x5019;&#x518D;&#x66F4;&#x65B0;&#x7236;&#x7EC4;&#x4EF6;&#x89C6;&#x56FE;&#x7684;&#x5012;&#x8BA1;&#x65F6;&#x5C31;&#x5DF2;&#x7ECF;&#x592A;&#x665A;&#x4E86;&#x3002;Angular &#x7684;&#x5355;&#x5411;&#x6570;&#x636E;&#x6D41;&#x89C4;&#x5219;&#x4F1A;&#x963B;&#x6B62;&#x5728;&#x540C;&#x4E00;&#x4E2A;&#x5468;&#x671F;&#x5185;&#x66F4;&#x65B0;&#x7236;&#x7EC4;&#x4EF6;&#x89C6;&#x56FE;&#x3002;&#x5E94;&#x7528;&#x5728;&#x663E;&#x793A;&#x79D2;&#x6570;&#x4E4B;&#x524D;&#x4F1A;&#x88AB;&#x8FEB;&#x518D;&#x7B49;&#x4E00;&#x8F6E;&#x3002;</p>
<p>   &#x4F7F;&#x7528; <code>setTimeout()</code> &#x6765;&#x7B49;&#x4E0B;&#x4E00;&#x8F6E;&#xFF0C;&#x7136;&#x540E;&#x6539;&#x5199; <code>seconds</code> &#x65B9;&#x6CD5;&#xFF0C;&#x8FD9;&#x6837;&#x5B83;&#x63A5;&#x4E0B;&#x6765;&#x5C31;&#x4F1A;&#x4ECE;&#x6CE8;&#x5165;&#x7684;&#x8FD9;&#x4E2A;&#x8BA1;&#x65F6;&#x5668;&#x7EC4;&#x4EF6;&#x91CC;&#x83B7;&#x53D6;&#x79D2;&#x6570;&#x7684;&#x503C;&#x3002; </p>
<ul>
<li>&#x7236;&#x7EC4;&#x4EF6;&#x548C;&#x5B50;&#x7EC4;&#x4EF6;&#x901A;&#x8FC7;&#x670D;&#x52A1;&#x6765;&#x901A;&#x8BAF;   </li>
</ul>
<p>&#x7236;&#x7EC4;&#x4EF6;&#x548C;&#x5B83;&#x7684;&#x5B50;&#x7EC4;&#x4EF6;&#x5171;&#x4EAB;&#x540C;&#x4E00;&#x4E2A;&#x670D;&#x52A1;&#xFF0C;&#x5229;&#x7528;&#x8BE5;&#x670D;&#x52A1;&#x5728;&#x5BB6;&#x5EAD;&#x5185;&#x90E8;&#x5B9E;&#x73B0;&#x53CC;&#x5411;&#x901A;&#x8BAF;&#x3002;</p>
<p>&#x8BE5;&#x670D;&#x52A1;&#x5B9E;&#x4F8B;&#x7684;&#x4F5C;&#x7528;&#x57DF;&#x88AB;&#x9650;&#x5236;&#x5728;&#x7236;&#x7EC4;&#x4EF6;&#x548C;&#x5176;&#x5B50;&#x7EC4;&#x4EF6;&#x5185;&#x3002;&#x8FD9;&#x4E2A;&#x7EC4;&#x4EF6;&#x5B50;&#x6811;&#x4E4B;&#x5916;&#x7684;&#x7EC4;&#x4EF6;&#x5C06;&#x65E0;&#x6CD5;&#x8BBF;&#x95EE;&#x8BE5;&#x670D;&#x52A1;&#x6216;&#x8005;&#x4E0E;&#x5B83;&#x4EEC;&#x901A;&#x8BAF;&#x3002;</p>
<p>&#x670D;&#x52A1;</p>
<pre><code>    import { Injectable } from &apos;@angular/core&apos;;
    import { Subject }    from &apos;rxjs&apos;;

    @Injectable()
    export class MissionService {

      // Observable string sources
      private missionAnnouncedSource = new Subject&lt;string&gt;();
      private missionConfirmedSource = new Subject&lt;string&gt;();

      // Observable string streams
      missionAnnounced$ = this.missionAnnouncedSource.asObservable();
      missionConfirmed$ = this.missionConfirmedSource.asObservable();

      // Service message commands
      announceMission(mission: string) {
        this.missionAnnouncedSource.next(mission);
      }

      confirmMission(astronaut: string) {
        this.missionConfirmedSource.next(astronaut);
      }
    }
</code></pre><p>&#x63D0;&#x4F9B;&#x670D;&#x52A1;&#x7684;&#x5B9E;&#x4F8B;&#xFF0C;&#x5E76;&#x5C06;&#x5176;&#x5171;&#x4EAB;&#x7ED9;&#x5B83;&#x7684;&#x5B50;&#x7EC4;&#x4EF6;(&#x901A;&#x8FC7; <code>providers</code> &#x5143;&#x6570;&#x636E;&#x6570;&#x7EC4;)&#xFF0C;&#x5B50;&#x7EC4;&#x4EF6;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x6784;&#x9020;&#x51FD;&#x6570;&#x5C06;&#x8BE5;&#x5B9E;&#x4F8B;&#x6CE8;&#x5165;&#x5230;&#x81EA;&#x8EAB;&#x3002;</p>
<pre><code>    import { Component }          from &apos;@angular/core&apos;;

    import { MissionService }     from &apos;./mission.service&apos;;

    @Component({
      selector: &apos;app-mission-control&apos;,
      template: `
        &lt;h2&gt;Mission Control&lt;/h2&gt;
        &lt;button (click)=&quot;announce()&quot;&gt;Announce mission&lt;/button&gt;
        &lt;app-astronaut *ngFor=&quot;let astronaut of astronauts&quot;
          [astronaut]=&quot;astronaut&quot;&gt;
        &lt;/app-astronaut&gt;
        &lt;h3&gt;History&lt;/h3&gt;
        &lt;ul&gt;
          &lt;li *ngFor=&quot;let event of history&quot;&gt;&lt;/li&gt;
        &lt;/ul&gt;
      `,
      providers: [MissionService]
    })
    export class MissionControlComponent {
      astronauts = [&apos;Lovell&apos;, &apos;Swigert&apos;, &apos;Haise&apos;];
      history: string[] = [];
      missions = [&apos;Fly to the moon!&apos;,
                  &apos;Fly to mars!&apos;,
                  &apos;Fly to Vegas!&apos;];
      nextMission = 0;

      constructor(private missionService: MissionService) {
        missionService.missionConfirmed$.subscribe(
          astronaut =&gt; {
            this.history.push(`${astronaut} confirmed the mission`);
          });
      }

      announce() {
        let mission = this.missions[this.nextMission++];
        this.missionService.announceMission(mission);
        this.history.push(`Mission &quot;${mission}&quot; announced`);
        if (this.nextMission &gt;= this.missions.length) { this.nextMission = 0; }
      }
    }
</code></pre><p><code>AstronautComponent</code> &#x4E5F;&#x901A;&#x8FC7;&#x81EA;&#x5DF1;&#x7684;&#x6784;&#x9020;&#x51FD;&#x6570;&#x6CE8;&#x5165;&#x8BE5;&#x670D;&#x52A1;&#x3002;&#x7531;&#x4E8E;&#x6BCF;&#x4E2A; <code>AstronautComponent</code> &#x90FD;&#x662F;<code>MissionControlComponent</code> &#x7684;&#x5B50;&#x7EC4;&#x4EF6;&#xFF0C;&#x6240;&#x4EE5;&#x5B83;&#x4EEC;&#x83B7;&#x53D6;&#x5230;&#x7684;&#x4E5F;&#x662F;&#x7236;&#x7EC4;&#x4EF6;&#x7684;&#x8FD9;&#x4E2A;&#x670D;&#x52A1;&#x5B9E;&#x4F8B;&#x3002;</p>
<pre><code>    import { Component, Input, OnDestroy } from &apos;@angular/core&apos;;

    import { MissionService } from &apos;./mission.service&apos;;
    import { Subscription }   from &apos;rxjs&apos;;

    @Component({
      selector: &apos;app-astronaut&apos;,
      template: `
        &lt;p&gt;
          : &lt;strong&gt;&lt;/strong&gt;
          &lt;button
            (click)=&quot;confirm()&quot;
            [disabled]=&quot;!announced || confirmed&quot;&gt;
            Confirm
          &lt;/button&gt;
        &lt;/p&gt;
      `
    })
    export class AstronautComponent implements OnDestroy {
      @Input() astronaut: string;
      mission = &apos;&lt;no mission announced&gt;&apos;;
      confirmed = false;
      announced = false;
      subscription: Subscription;

      constructor(private missionService: MissionService) {
        this.subscription = missionService.missionAnnounced$.subscribe(
          mission =&gt; {
            this.mission = mission;
            this.announced = true;
            this.confirmed = false;
        });
      }

      confirm() {
        this.confirmed = true;
        this.missionService.confirmMission(this.astronaut);
      }

      ngOnDestroy() {
        // prevent memory leak when component destroyed
        this.subscription.unsubscribe();
      }
    }
</code></pre>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="cycle.html" class="navigation navigation-prev " aria-label="Previous page: 生命周期钩子">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="style.html" class="navigation navigation-next " aria-label="Next page: 组件样式">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"组件交互","level":"1.4.3","depth":2,"next":{"title":"组件样式","level":"1.4.4","depth":2,"path":"comAndtem/style.md","ref":"comAndtem/style.md","articles":[]},"previous":{"title":"生命周期钩子","level":"1.4.2","depth":2,"path":"comAndtem/cycle.md","ref":"comAndtem/cycle.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["fontsettings","sharing","lunr","search","highlight","livereload"],"pluginsConfig":{"fontsettings":{"family":"sans","size":2,"theme":"white"},"sharing":{"all":["facebook","google","twitter","weibo","instapaper"],"facebook":true,"google":false,"instapaper":false,"twitter":true,"vk":false,"weibo":false},"lunr":{"ignoreSpecialCharacters":false,"maxIndexSize":1000000},"search":{},"highlight":{},"livereload":{},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"comAndtem/component.md","mtime":"2018-06-14T06:16:05.329Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2018-06-29T06:02:19.129Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    

    </body>
</html>

